/**
 * 
 */
package com.jie.eclipse.pan.gef.dialog;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import com.jie.eclipse.pan.bind.PageDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;
import com.jie.eclipse.pan.listener.PMouseListener;
import com.jie.eclipse.pan.swtitem.ListGroup;

/**
 * @author yanjie
 *
 */
public class LogDialog extends TitleAreaDialog{
	private BaseDataEntity record = null;
	private BaseDataEntity ret = null;
	/**
	 * @return the ret
	 */
	public BaseDataEntity getRet() {
		if(ret == null){
			ret = new BaseDataEntity();
		}
		return ret;
	}

	private PageDataBind bind = null;
	/**
	 * @return the bind
	 */
	public PageDataBind getDataBind() {
		return bind;
	}

	/**
	 * @param parentShell
	 * @param data
	 * @param action
	 */
	public LogDialog(Shell parentShell, BaseDataEntity data) {
		super(parentShell);
		this.record = data;
		bind = new PageDataBind();
	}
	
	@Override
	protected Control createContents(Composite parent) {
		super.createContents(parent);
		this.getShell().setText("日志处理对话框");// 设置对话框标题栏
		this.setTitle("日志处理");// 设置标题信息
		this.setMessage("请输处理日志状态或转交他人处理", IMessageProvider.INFORMATION);// 设置初始化对话框的提示信息
		//设置右侧的图片，一般为48*48大小
		this.setTitleImage(PanImageHelper.getInstance().getImage("icons/pan.jpg"));
		return parent;
	}
	
	
	@Override
	protected Point getInitialSize() {
		// Point point = super.getInitialSize();
		// point.x = 600;
		return new Point(600, 600);
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		ScrolledForm  form = new ScrolledForm(parent, SWT.BORDER | SWT.V_SCROLL);
		form.setLayout(new GridLayout(1, false));
		form.setLayoutData(new GridData(GridData.FILL_BOTH));
		form.setAlwaysShowScrollBars(true);  
		form.setExpandHorizontal(true);
		form.setExpandVertical(true);
		form.setMinHeight(420);  
		
		FormToolkit tk = new FormToolkit(form.getDisplay()); 
		
		
		Composite body = form.getBody();
		body.setLayout( new TableWrapLayout());
		body.setBackground(bgcolor);
		body.setLayout(new GridLayout(1, false));
		body.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		Group group1 = new Group(body, SWT.NULL);
		group1.setText("基本信息");
		group1.setLayout(new GridLayout(4, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		
		Label lb1 =tk.createLabel(group1, "主机:");
		lb1.setBackground(bgcolor);
		Text hostip =tk.createText(group1, "",SWT.READ_ONLY);
		hostip.setBackground(bgcolor);
		hostip.setLayoutData(new GridData(100, 12));
		bind.bind("hostip", hostip);
		
		Label lb2 =tk.createLabel(group1, "路径:");
		lb2.setBackground(bgcolor);
		Text url =tk.createText(group1, "",SWT.READ_ONLY);
		url.setBackground(bgcolor);
		url.setLayoutData(new GridData(250, 12));
		bind.bind("url", url);
		
		Label lb3 =tk.createLabel(group1, "负责人:");
		lb3.setBackground(bgcolor);
		Text owner =tk.createText(group1, "",SWT.READ_ONLY);
		owner.setBackground(bgcolor);
		owner.setLayoutData(new GridData(100, 12));
		bind.bind("owner", owner);
		
		Label lb4 =tk.createLabel(group1, "页面名称:");
		lb4.setBackground(bgcolor);
		Text pagename =tk.createText(group1, "",SWT.READ_ONLY);
		pagename.setBackground(bgcolor);
		pagename.setLayoutData(new GridData(250, 12));
		bind.bind("pagename", pagename);
		
		Label lb5 =tk.createLabel(group1, "建立日期:");
		lb5.setBackground(bgcolor);
		Text crtday =tk.createText(group1, "",SWT.READ_ONLY);
		crtday.setBackground(bgcolor);
		crtday.setLayoutData(new GridData(100, 12));
		bind.bind("crtday", crtday);
		
		Label lb6 =tk.createLabel(group1, "修改日期:");
		lb6.setBackground(bgcolor);
		Text updday =tk.createText(group1, "",SWT.READ_ONLY);
		updday.setBackground(bgcolor);
		updday.setLayoutData(new GridData(250, 12));
		bind.bind("updday", updday);
		
		
		Label lb7 =tk.createLabel(group1, "状态:");
		lb7.setBackground(bgcolor);
		ListGroup status = new ListGroup(group1, "radio");
		status.setLayout(new GridLayout(2, false));
		status.setData("xtype", "ridio");
		Button btn1 = status.getNewButton();
		btn1.setData("0");
		btn1.setText("未处理");
		Button btn11= status.getNewButton();
		btn11.setData("1");
		btn11.setText("已处理");
		bind.bind("status_code", status);
		
		Label lb8 =tk.createLabel(group1, "重新指定给:");
		lb8.setBackground(bgcolor);
		Combo reasignto = new Combo(group1, SWT.READ_ONLY);
		String[][] fzrs = QMDao.getInstance().getPanLogFZR(true);
		reasignto.setItems(fzrs[0]);
		reasignto.select(0);
		reasignto.setLayoutData(new GridData(100, 24));
		for (int i = 0; i < fzrs[1].length; i++) {
			reasignto.setData(String.valueOf(i), fzrs[1][i]);
		}
		bind.bind("reasignto",reasignto);
		
		Label lb9 =tk.createLabel(group1, "日志明细:");
		Text detail =tk.createText(group1, "",SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		detail.setBackground(bgcolor);
		GridData detailGridData = new GridData();
		detailGridData.horizontalSpan = 3;
		detailGridData.heightHint = 300;
		detailGridData.widthHint = 410;
		detail.setLayoutData(detailGridData);
		bind.bind("detail", detail);
		
		return parent;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createButton(org.eclipse.swt.widgets.Composite, int, java.lang.String, boolean)
	 */
	@Override
	protected Button createButton(Composite parent, int id, String label,
			boolean defaultButton) {
		Image img = null;
		if(id == TitleAreaDialog.OK){
			label = "保存";
			img = PanImageHelper.getInstance().getImage("icons/icon_save.gif");
		}else if(id == TitleAreaDialog.CANCEL){
			label = "取消";
			img = PanImageHelper.getInstance().getImage("icons/icon_cancel.gif");
		}
		Button button = super.createButton(parent, id, label, defaultButton);
		button.setImage(img);
		button.addMouseListener(new PMouseListener(){
			@Override
			public void mouseUp(MouseEvent paramMouseEvent) {
				getRet().spsv("reasignto", getDataBind().getValue("reasignto"));
				getRet().spsv("status_code", getDataBind().getValue("status_code"));
			}
		});
        return button;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#initializeBounds()
	 */
	@Override
	protected void initializeBounds() {
		super.initializeBounds();
		bind.setRecord(record);
	}
}
